package com.gao.service.impl;

import com.gao.dao.UserMapper;
import com.gao.entity.User;
import com.gao.entity.UserExample;
import com.gao.service.IUserService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

/**
 * @author gao
 * @time 2020/05/28 17:11:28
 */
@Transactional
@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    private UserMapper mapper;

    @Override
    public void save(User user) {
        mapper.insert(user);
    }

    @Override
    public void delete(Integer id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public void update(User user) {
        mapper.updateByPrimaryKey(user);
    }

    @Override
    public List<User> findAll() {
        return mapper.selectByExample(null);
    }

    @Override
    public User findById(Integer id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public List<User> search(UserExample ue, int... pages) {
        if (pages.length == 1) {
            throw new RuntimeException("分页数据不完善: " + Arrays.toString(pages));
        }
        if (pages.length > 1) {
            PageHelper.startPage(pages[0], pages[1]);
        }
        return mapper.selectByExample(ue);
    }

}
